<?php
  /** Manipule du code source depuis le code source ...
   * auteur : sebastien mosser
   */ 

class SourceCode {


	private $_fileName = "";
	
	public function __construct($f) {
		if (file_exists($f))
			$this->_fileName = $f;
		else
			throw new RuntimeException("SourceCode::construct","File not found !");
	}
	
	public function highlight() {
		//$source = php_strip_whitespace($this->_fileName);
		// return highlight_string($source, True);
		return highlight_file($this->_fileName,True);
	}
	
	
	public static function bajax_display($param) {
	  // On rajoute de la sécurité !
	  $acl = array();
	  $acl[] = 7;
	  Bajax::secureInvoke($acl);
	  
		$f = new SourceCode(BALLOON_FS_PATH."kernel/".$param[0]);
		$kernel = "Kernel ";
		$title = str_replace("./", $kernel . " >> ",$param[0]);
		$title = str_replace("/", " >> ",$title);
		$ans = "<h2> " . $title . " </h2> ";
		$ans .= $f->highlight();
		return $ans;
	}
	
	public static function listKernelFiles() {
		
		chdir(BALLOON_FS_PATH."kernel");
		return self::displayDir(".");
	}
		
	
	private static function displayDir($dirName){
		$dir = opendir($dirName);
		$dirId = "dirId".$dirName;
		$ans = "\n<!-- Directory BEGIN : ".$dirName."-->\n";
		if ($dirName != ".") 
		  $ans .= "\n<ul id=\"".$dirId."\" style=\"display: none\">\n";
		else
		  $ans .= "\n<ul id=\"".$dirId."\">\n";
		
		$tmp = "";
		while(($f = readdir($dir)) != False) {
			
			if (is_dir($f) && $f != "." && $f != ".." && $f != ".svn") {
				$tmp .= "  <li> <span onClick=\"manage('$f');\" class=\"error\" >$f </span>: \n";
				$tmp .= self::displayDir($dirName."/".$f) . "</li>";
			}
			
			$length = strlen($f);
			if ($length > 4 && $f[$length-1] == 'p' && $f[$length-2] == 'h' && $f[$length-3] == 'p' && $f[$length-4] == '.')
				$tmp .= "  <li> <a onClick=\"displaySource('".$dirName."/".$f."')\">$f</a></li>\n";
		}
		
		if ($tmp == "")
		  $ans .= "<li> <em>Vide ...</em></li>";
		else
			$ans .= $tmp;
		//	return;
		$ans .= "</ul>\n";
		$ans .= "\n<!-- Directory END : ".$dirName."-->\n";
		return $ans;
		
	}
	

}

?>